<?php
# session_name(DicomScience);
# session_start();

/*
 * The readBlog.php Controller listens on http_get option "lastdays" a countable numer without
 * decimales. The abount of last days is multiplied by the numer of seconds each day. Compared
 * with the current time index of the server, the server response will be the amount and
 * content of the blog database within the amount of days.
 */

$lastDays = $_GET['lastdays'];
$blogNumber = $_GET['blog'];

/*
 * ACTION TRIGGER: which response is appropriate?
 */
if (! $lastDays == ""){
	# See Section GETBLOGLIST
	getBlogList($lastDays);
} elseif (! $blogNumber == ""){
	# See Section SHOWBLOGBYID
	getBlogById($blogNumber);
}

/*
 * GETBLOGLIST
 */
function getBlogList($days){
	// mapping variables :-)
	$lastdays = $days;
		
	$now = time();
	$oneday = 86400;
	$range = $lastdays * $oneday;
	$lastdate = $now - $range;
	
	// Getting global settings
	require_once('../includes/gset.php');
	
	// Defining response language
	$session_language = $_SESSION['lang'];
	if ($session_language == "") {
		
		// Assuming Controller has not been triggered by a PHP Session
		if ($lang == 1)	{
	
			require_once ('../includes/language/german.php');	
				
		} elseif($lang == 2){
	
			require_once ('../includes/language/english.php');
		
		} elseif($lang == 3){
	
			require_once ('../includes/language/french.php');
		
		}	
		
	}
	
	
	// Performing Database Query
	require_once('../includes/gset.php');
	require_once('../includes/database/connect.php');
	
	$sql_getblogs = "SELECT id_blog, blog.id_staff, blogdate, message, lastname, firstname FROM blog, staff WHERE blogdate > '$lastdate' AND blog.id_staff = staff.id_staff ORDER BY (id_blog) DESC;";
	$getblogs = mysql_query($sql_getblogs);
	
	require_once('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<news>";
	
	while ($blog = mysql_fetch_object($getblogs)){
		$idblog = $blog->id_blog;
		$editor = $blog->id_staff;
		$blogdate = $blog->blogdate;
		$message = $blog->message;
		$editorlastname = $blog->lastname;
		$editorfirstname = $blog->firstname;
				
		$aktuelle_zeit = time();
		$lessoneminute = $aktuelle_zeit - 60;
		$lessonehour = $aktuelle_zeit - 3600;
		$lessoneday = $aktuelle_zeit - 86400;
		$yesterday = $aktuelle_zeit - 172800;
		$week = $aktuelle_zeit - 604800;
		$twoweeks = $aktuelle_zeit - 1209600;
		$month = $aktuelle_zeit - 2419200;
		$twomonths = $aktuelle_zeit - 4838400;
		$halfyear = $aktuelle_zeit - 14515200;
		$year = $aktuelle_zeit - 29030400;	
						
		if ($blogdate > $lessoneminute){
		$display_blogdate = $nls_lessoneminute;
		} elseif ($blogdate > $lessonehour){
		$display_blogdate = $nls_lessonehour;
		} elseif ($blogdate > $lessoneday){
		$display_blogdate = $nls_lessoneday;
		} elseif ($blogdate > $yesterday) {
		$display_blogdate = $nls_yesterday;
		} elseif ($blogdate > $week){
		$display_blogdate = $nls_week;
		} elseif ($blogdate > $twoweeks){
		$display_blogdate = $nls_twoweeks;
		} elseif ($blogdate > $month){
		$display_blogdate = $nls_month;
		} elseif ($blogdate > $twomonths){
		$display_blogdate = $nls_twomonths;
		} elseif ($blogdate > $halfyear){
		$display_blogdate = $nls_halfyear;
		} elseif ($blogdate > $year){
		$display_blogdate = $nls_year;
		} else {
		$display_blogdate = $nls_older;
		}
		echo "<message>";
			echo "<id>";
			echo $idblog;
			echo "</id>";
			echo "<text>";
			echo $message;
			echo "</text>";	
			echo "<editor>";
			echo $editorfirstname." ".$editorlastname;
			echo "</editor>";
			echo "<editorID>";
			echo $editor;
			echo "</editorID>";
			echo "<date>";
			echo $display_blogdate;
			echo "</date>";
		echo "</message>";
	}
	// Finalizing XML Response
	echo "</news>";
}


/*
 * SECTION SHOWBLOGBYID
 */
function getBlogById($id){
	
	$now = time();
	$oneday = 86400;
	$range = $lastdays * $oneday;
	$lastdate = $now - $range;
	
	// Getting global settings
	require_once('../includes/gset.php');
	
	// Defining response language
	$session_language = $_SESSION['lang'];
	if ($session_language == "") {
		
		// Assuming Controller has not been triggered by a PHP Session
		if ($lang == 1)	{
	
			require_once ('../includes/language/german.php');	
				
		} elseif($lang == 2){
	
			require_once ('../includes/language/english.php');
		
		} elseif($lang == 3){
	
			require_once ('../includes/language/french.php');
		
		}	
		
	}
	
	
	// Performing Database Query
	require_once('../includes/gset.php');
	require_once('../includes/database/connect.php');
	
	$sql_getblogs = "SELECT id_blog, blog.id_staff, blogdate, message, lastname, firstname FROM blog, staff WHERE id_blog = '$id' AND blog.id_staff = staff.id_staff;";
	$getblogs = mysql_query($sql_getblogs);
	
	require_once('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<news>";
	
	while ($blog = mysql_fetch_object($getblogs)){
	
		$idblog = $blog->id_blog;
		$editor = $blog->id_staff;
		$blogdate = $blog->blogdate;
		$message = $blog->message;
		$editorlastname = $blog->lastname;
		$editorfirstname = $blog->firstname;
				
		$aktuelle_zeit = time();
		$lessoneminute = $aktuelle_zeit - 60;
		$lessonehour = $aktuelle_zeit - 3600;
		$lessoneday = $aktuelle_zeit - 86400;
		$yesterday = $aktuelle_zeit - 172800;
		$week = $aktuelle_zeit - 604800;
		$twoweeks = $aktuelle_zeit - 1209600;
		$month = $aktuelle_zeit - 2419200;
		$twomonths = $aktuelle_zeit - 4838400;
		$halfyear = $aktuelle_zeit - 14515200;
		$year = $aktuelle_zeit - 29030400;	
						
		if ($blogdate > $lessoneminute){
		$display_blogdate = $nls_lessoneminute;
		} elseif ($blogdate > $lessonehour){
		$display_blogdate = $nls_lessonehour;
		} elseif ($blogdate > $lessoneday){
		$display_blogdate = $nls_lessoneday;
		} elseif ($blogdate > $yesterday) {
		$display_blogdate = $nls_yesterday;
		} elseif ($blogdate > $week){
		$display_blogdate = $nls_week;
		} elseif ($blogdate > $twoweeks){
		$display_blogdate = $nls_twoweeks;
		} elseif ($blogdate > $month){
		$display_blogdate = $nls_month;
		} elseif ($blogdate > $twomonths){
		$display_blogdate = $nls_twomonths;
		} elseif ($blogdate > $halfyear){
		$display_blogdate = $nls_halfyear;
		} elseif ($blogdate > $year){
		$display_blogdate = $nls_year;
		} else {
		$display_blogdate = $nls_older;
		}
		echo "<message>";
			echo "<text>";
			echo $message;
			echo "</text>";	
			echo "<editor>";
			echo $editorfirstname." ".$editorlastname;
			echo "</editor>";
			echo "<editorID>";
			echo $editor;
			echo "</editorID>";
			echo "<date>";
			echo $display_blogdate;
			echo "</date>";
		echo "</message>";
	}
	// Finalizing XML Response
	echo "</news>";
	
} 

?>
